<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">

        <el-form-item label="工号" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入工号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="姓名" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            clearable
            size="small"
            v-model="queryParams.startTime"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            clearable
            size="small"
            v-model="queryParams.endTime"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="部门" prop="deptName">
          <el-input
            v-model="queryParams.deptName"
            placeholder="请输入工作站编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['hr:wageStatistics:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch"></right-toolbar>
      </el-row>
    </el-card>

    <el-card style="border-radius: 10px">
      <el-table v-loading="loading" :data="wageStatisticsList" border>
        <el-table-column label="工号" align="center" prop="userName" />
        <el-table-column label="姓名" align="center" prop="nickName" />
        <el-table-column label="所在部门" align="center" prop="deptName" />
        <el-table-column label="工资总额" align="center" prop="totalSalary" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                @click="handleList(scope.row)"
                v-hasPermi="['hr:wageStatistics:detail:list']"
            >明细</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-card style="border-radius: 10px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleDetailExport"
                v-hasPermi="['hr:wageStatistics:detail:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch"></right-toolbar>
        </el-row>
      </el-card>

      <el-card style="border-radius: 10px">
        <el-table v-loading="detailLoading" :data="wageStatisticsDetailList" >
          <el-table-column label="姓名" align="center" prop="userName" />
          <el-table-column label="生产任务号" align="center" prop="taskCode" />
          <el-table-column label="任务名称" align="center" prop="taskName" />
          <el-table-column label="实际完工时间" align="center" prop="actualEndTime" />
          <el-table-column label="计价方式" align="center" prop="pricingMethod">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.pricingMethod === 'TIME'">计时</el-tag>
              <el-tag v-if="scope.row.pricingMethod === 'PIECEWORK'">计件</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="标准单价" align="center" prop="price" />
          <el-table-column label="实际数量（时间）" align="center" prop="actualQuantity" />
          <el-table-column label="实际工资" align="center" prop="actualSalary" />
        </el-table>
        <pagination
            v-show="detailTotal>0"
            :total="detailTotal"
            :page.sync="detailParams.pageNum"
            :limit.sync="detailParams.pageSize"
            @pagination="getDetailList"
        />
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { listWageStatistics, listWageStatisticsDetail } from "@/api/mes/hr/wageStatistics";

export default {
  name: "WageStatistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计价工资统计表格数据
      wageStatisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        deptName: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 计价工资统计明细表格数据
      wageStatisticsDetailList: [],
      // 明细遮罩层
      detailLoading: true,
      // 明细总条数
      detailTotal: 0,
      // 明细查询参数
      detailParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        startTime: null,
        endTime: null,
        taskCode: null,
        taskName: null,
        actualEndTime: null,
        pricingMethod: null,
        price: null,
        actualQuantity: null,
        actualSalary: null,
      },
      userName: null,
    };
  },
  created() {
    this.loading = false;
  },
  methods: {
    /** 查询计价工资标准列表 */
    getList() {
      this.loading = true;
      listWageStatistics(this.queryParams).then(response => {
        this.wageStatisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userName: null,
        nickName: null,
        deptName: null,
        startTime: this.queryParams.startTime,
        endTime: this.queryParams.endTime,
        taskCode: null,
        taskName: null,
        actualEndTime: null,
        pricingMethod: null,
        price: null,
        actualQuantity: null,
        actualSalary: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/hr/wageStatistics/exportList', {
        ...this.queryParams
      }, `计价工资统计_${new Date().getTime()}.xlsx`)
    },
    // 打开明细弹出框
    handleList(row) {
      this.userName = null;
      this.reset();
      this.userName = row.nickName;
      this.getDetailList();
    },
    /** 明细查询按钮操作 */
    getDetailList() {
      this.detailParams.userName = this.userName;
      this.detailParams.startTime = this.queryParams.startTime;
      this.detailParams.endTime = this.queryParams.endTime;
      listWageStatisticsDetail(this.detailParams).then(response => {
        this.wageStatisticsDetailList = response.rows;
        this.detailTotal = response.total;
        this.open = true;
        this.title = "计价工资明细";
        this.detailLoading = false;
      })
    },
    /** 明细导出按钮操作 */
    handleDetailExport() {
      this.download('/hr/wageStatistics/exportDetailList', {
        ...this.detailParams
      }, `计价工资明细统计_${new Date().getTime()}.xlsx`)
    },


  },
};
</script>
